const { Controller } = require('egg');
const fs=require('fs')
const path=require('path');
const { title } = require('process');

class HomeController extends Controller {
  async getData() {
    const { ctx } = this;
    const file=JSON.parse(fs.readFileSync(path.join(__dirname,'/order.json')),{
        encoding:'utf-8'
    })
    ctx.body ={
        file
    };
  }
  async addDataItem() {
    const { ctx } = this;
    const {id,value,}=ctx.request.body
    const file=JSON.parse(fs.readFileSync(path.join(__dirname,'/order.json')),{
        encoding:'utf-8'
    })
    const item={
        id:String(Math.random()).slice(3,7),
        title:value
    }
    file.push(item)
    fs.writeFileSync((path.join(__dirname,'/order.json'),JSON.stringify(file)))
    ctx.body = {
        message:'添加成功'
    };
  }
  async editItem() {
    const { ctx } = this;
    const {id} =ctx.request.body
    const file=JSON.parse(fs.readFileSync(path.join(__dirname,'/order.json')),{
        encoding:'utf-8'
    })
    const newFile=file.map(item=>{})
    fs.writeFileSync((path.join(__dirname,'/order.json'),JSON.stringify(newFile)))
    ctx.body = {
        message:'修改成功'
    };
  }
  async delItem() {
    const { ctx } = this;
    const {id} =ctx.request.body
    const file=JSON.parse(fs.readFileSync(path.join(__dirname,'/order.json')),{
        encoding:'utf-8'
    })
    fs.writeFileSync((path.join(__dirname,'/order.json'),JSON.stringify(newFile)))
    ctx.body = {
        message:'删除成功'
    };
  }
}

module.exports = HomeController;